#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "motor.h"
#include "uart.h"
#include "stdio.h"
#include "string.h"
#include "makedata.h"
#include "rgbled.h"
#include "dht11.h"
#include "wifi.h"
#include "MQTT.h"
#include "cJSON.h"
#include "cjsonmake.h"
#include "stdlib.h"
#include "stepmotor.h"
#include "oled.h"
#include "time.h"

int main(void)
{
    uint8_t ret;						//验证WIFI接入
    char buf[512]={0};
    char *string=NULL;			//上传数据到云端的字符串容器
    int motor=0;						//电机速度
    int RGB[3]={0,50,0};		//彩灯RGB值
    char RBGbuf[128]={0};

    TYPE_DATA value;				//接收MotorStringMake函数返回的RGB及Msp
    TYPE_DHT11 dht11;				//温湿度数据容器

    OLEDInit( );						//OLED屏幕初始化
    OLED_Clear();						//OLED屏幕清除屏幕
    DisplayAsciiString(0,0, "Initing.....");//各模块开始初始化
    LedInit( );							//LED初始化
    MotorInit( );						//电机初始化
    RGBPinInit( );					//RGB彩灯初始化
    uart1_init(72,115200);	//串口1初始化
    Uart2Init(115200);
    DHT11PinInit( );				//温湿度传感器初始化
    StepMotorPinInit( );		//步进电机初始化 Mode：LEFT RIGHT
    ret=WifiInit( );				//WIFI初始化

    /****************************
    RGB数据格式："R:50,G:0,B:50"
    *****************************/
    RGBControl(RGB[0],RGB[1],RGB[2]);			//RGB彩灯初始颜色：红色
    MotorControl(motor);									//电机初始转速：0 r/s
    StepMotorContorl(LEFT,5);		//Mode:LEFT PAUSE RIGHT

    if(ret==0)
    {
        printf("wifi初始化成功！！！\r\n");
        _mqtt.Init(rxbuf,0,txbuf,0);//MQTT初始化
        
        //MQTT连接
        ret=_mqtt.Connect("zhangxueyi|securemode=3,signmethod=hmacsha1|",
            "test&a15MWs3AfDR",
            "F1C54036C5E676C6F646E006A801548AE1D0884A");	
        if(ret==1)
        {
            printf("MQTT接入成功\r\n");
            ret=_mqtt.SubscribeTopic("/sys/a15MWs3AfDR/test/thing/service/property/set",0,1);
            if(ret==1)
            printf("MQTT订阅成功\r\n");
            DisplayAsciiString(0,0,"init OK !!!");
            Delayms(300);
        }
    }
    else
        printf("wifi初始化失败！！！\r\n");


    printf("[%s][%d]\r\n",__func__,__LINE__);
    //GPIOB->ODR &= ~(1<<0);
    while(1)
    {
        ReadDHT11(&dht11);
        printf("%.2f C %.2fH\n",dht11.temp,dht11.humi);
        if(WifiRev.RevOver)         //接收收WIFI下发的数据
        {
            OLED_Clear();											//OLED屏幕清除屏幕
            
            value = MotorStringMake(WifiRev.WifiRevBuf);	//获取RGB及电机数据
            
            #if 0
            if(strstr((const char *)WifiRev.WifiRevBuf,"RGB")!= NULL) 
            {
                RGB[0] = value.red;
                RGB[1] = value.green;
                RGB[2] = value.blue;
                if(value.red == '1')
                {
                    //GPIOB->ODR &= ~(1<<0);
                }
                else
                {
                    //GPIOB->ODR |= (1<<0);
                }
            }
            if(strstr((const char *)WifiRev.WifiRevBuf,"MotorSpeed")!= NULL) 
                motor = value.speed;
            
            RGBControl(RGB[0],RGB[1],RGB[2]);	//RGB彩灯控制
            MotorControl(motor);							//电机转速控制
            StepMotorContorl(RIGHT,5);						//步进电机控制 Mode：LEFT RIGHT
            #endif
            if(strstr((const char *)WifiRev.WifiRevBuf,"AlarmSwitch")!= NULL)
            {
                if(value.LED)
                {
                    GPIOB->ODR &= ~(1<<0);   //开灯
                }
                else
                {
                    GPIOB->ODR |= (1<<0);   //关灯
                }
            }
            
            WifiRev.RevOver=0;								//标志清零
        }
        RGB[0] = 0; RGB[1] = 0 ;RGB[1] = 0;
        sprintf( RBGbuf,"R:%d G:%d B:%d",RGB[0],RGB[1],RGB[2]);
        DisplayAsciiString(0,0,RBGbuf);
        sprintf( buf,"Temp  : %.1f C",dht11.temp);
        DisplayAsciiString(2,0,buf);
        sprintf( buf,"Humi  : %.1f %%",dht11.humi);
        DisplayAsciiString(4,0,buf);
        sprintf( buf,"MotSpd: %d r/s",motor);
        DisplayAsciiString(6,0,buf);
        
        string=MyCreatJson(dht11,motor,RBGbuf);
        _mqtt.PublishData("/sys/a1jwTHmfQCU/Intellig_Home/thing/event/property/post", //具有发布权限的TOPIC
                                            string,    	//符合json数据格式的消息串
                                            0);  				//消息等级
        free(string);									//发布完释放空间
        
        Delayms(3000);
    }
}

